package com.example.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
import io.netty.util.CharsetUtil;
import com.google.gson.Gson;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/*
 *  @author 雨安
 *  类名： NettyServerHandler
 *  创建时间：2024/3/8
 * @Descrition：自定义服务端handler
 */
public class NettyServerHandler implements ChannelInboundHandler {
    @Override
    public void channelRegistered(ChannelHandlerContext channelHandlerContext) throws Exception {

    }

    @Override
    public void channelUnregistered(ChannelHandlerContext channelHandlerContext) throws Exception {

    }

    @Override
    public void channelActive(ChannelHandlerContext channelHandlerContext) throws Exception {

    }

    @Override
    public void channelInactive(ChannelHandlerContext channelHandlerContext) throws Exception {

    }

    /**
     * 通道读取事件
     * @param channelHandlerContext 通道上下文对象
     * @param o 消息
     * @throws Exception 抛出的异常
     */
    @Override
    public void channelRead(ChannelHandlerContext channelHandlerContext,Object o) throws Exception {
        System.out.println("进入了服务端");
        //接收数据
        ByteBuf byteBuf = (ByteBuf) o;
        //拿到对方发送的数据
        String a = byteBuf.toString(CharsetUtil.UTF_8);
        //设置截取开始
        String strat = "(";
        //设置截取尾部
        String end = ")";
        //截取数据
        String substring = a.substring(a.indexOf(strat)+1, a.indexOf(end));
        //拼接字符
        String json = "{" + substring + "}";
        //测试
        json = json.replace("=",":");
        //创建对象映射对象
        ObjectMapper obj = new ObjectMapper();
        //赋值
        Person person = obj.readValue(json,Person.class);
        //打印转换好的对象
        System.out.println("这是我转换后的数据" + person);
    }

    /**
     * 读取完毕事件
     * @param channelHandlerContext 通道上下文
     * @throws Exception 抛出的异常
     */
    @Override
    public void channelReadComplete(ChannelHandlerContext channelHandlerContext) throws Exception {
        channelHandlerContext.writeAndFlush(Unpooled.copiedBuffer("你好,我是服务端",CharsetUtil.UTF_8));
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {

    }

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext channelHandlerContext) throws Exception {

    }

    @Override
    public void handlerAdded(ChannelHandlerContext channelHandlerContext) throws Exception {

    }

    @Override
    public void handlerRemoved(ChannelHandlerContext channelHandlerContext) throws Exception {

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable throwable) throws Exception {

    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Person{
    private String a;
}
